angular.module("app").controller("homeCtrl", ["$scope", "$http", "$state", "$filter", "$timeout","qiniuToken", function ($scope, $http, $state, $filter, $timeout,qiniuToken) {
    if (sessionStorage.userInfo) {
        $scope.userInfo = JSON.parse(sessionStorage.userInfo);
    }
    else {
        location.href = "#/login";
    }
    $scope.qiniuToken = qiniuToken.data.qiniuToken;
    $scope.roleDict= {
        1:{text:"系统管理"},
        2:{text:"APP管理"},
        3:{text:"素材管理"},
        4:{text:"社团管理"},
        100001:{text:"重置密码"},
    };
    $scope.selectedItems = [];
    $scope.accordionDataSource = [
        {
            id: 1,
            title: "APP管理",
            menus: [{name: "话题列表", value: "home.topic.list"}
            ]
        },
        {
            id: 2,
            title: "权限管理",
            menus: [{name: "用户列表",value: "home.user.manageList"}]
            // menus: [{name: "角色列表", value: "home.role.list"},{name: "用户列表",value: "home.user.manageList"}]
        },
        {
            id: 3,
            title: "福利管理",
            menus: [{name: "福利列表", value: "home.welfare.list"},{name: "福利券列表",value: "home.welfare.welfareItemList"}]
        },
        {
            id: 4,
            title: "个人设置",
            menus: [{name: "修改密码", value: "home.user.changePassword"},{name: "退出",value: ""}]
        }
    ];
    // if($scope.userInfo.role == 2){
    //     $scope.accordionDataSource.push({
    //         id:3,
    //         title:"系统设置",
    //         menus:[{name:"机构信息设置",value:"home.setting"}]
    //     })
    // }
    $scope.accordionOptions = {
        dataSource: $scope.accordionDataSource,
        animationDuration: 300,
        collapsible: true,
        bindingOptions: {
            selectedItems: "selectedItems"
        }
    };

    $scope.$on("$stateChangeSuccess", function (event, toState, toParams, fromState, fromParams) {
        if (toState.name.indexOf("topic") >= 0) {
            $scope.selectedItems = [$scope.accordionDataSource[0]];
        }
        else if (toState.name.indexOf("home.role.list") >= 0 || toState.name.indexOf("home.user.manageList") >= 0 || toState.name.indexOf("home.user.manageInfo") >= 0) {
            $scope.selectedItems = [$scope.accordionDataSource[1]];
        }
        else if (toState.name.indexOf("home.welfare.list") >= 0 || toState.name.indexOf("home.welfare.welfareItemList") >= 0) {
            $scope.selectedItems = [$scope.accordionDataSource[2]];
        }
        else if(toState.name.indexOf("home.user.changePassword") >= 0){
            $scope.selectedItems = [$scope.accordionDataSource[3]];
        }
    });

    $scope.logout = function () {
        if (confirm("确定要退出吗?")) {
            sessionStorage.clear();
            location.href = "#/login";
        }
    };
    $scope.eventStop = function ($event) {
        $event.stopPropagation();
    };
    $scope.goBack = function () {
        history.back();
    };
    // $scope.transformTreeData = function (data) {
    //     var arr = [];
    //     var obj = {};
    //     data.forEach(function (item) {
    //         item.href = item.orgId;
    //         item.id = item.orgId;
    //         item.text = item.orgName;
    //         item.expanded = true;
    //         obj[item.orgId] = item;
    //         if (item.orgId == $scope.userInfo.orgId) {
    //             arr.push(item);
    //         }
    //         else if (item.orgParent) {
    //             if (!obj[item.orgParent].items) {
    //                 obj[item.orgParent].items = [];
    //             }
    //             item.expanded = false;
    //             obj[item.orgParent].items.push(item);
    //         }
    //     });
    //     return arr;
    // };
    $scope.city = city;
    $scope.District = District;
    $scope.province = province;
    //
    // //District.forEach(function(item){
    // //    $scope.District[item.Id] = item;
    // //});
    // //city.forEach(function(item){
    // //    $scope.city[item.CityID] = item;
    // //});
    // //province.forEach(function(item){
    // //    $scope.province[item.ProID] = item;
    // //});
    //
    // $scope.treeViewConfig = {
    //     bindingOptions: {
    //         dataSource: "treeViewConfig.dataSource"
    //     },
    //     expandAllEnabled: true
    // };
    // $scope.treeViewDataSource = $scope.transformTreeData(treeViewDataSource.data);
    // $scope.treeViewDataSource[0].selected = true;
    // $scope.treeViewConfig.dataSource = $scope.treeViewDataSource;
    // $scope.orgList = {};
    // orgList.data.forEach(function (item) {
    //     $scope.orgList[item.orgId] = item;
    // });
    //
    // $scope.verifyTabs = {"0":true,"2":false,"-1":false};

    $scope.uploadPhoto = function(file,item){
        var form = new FormData();
        form.append("file", file);
        var xhr = new XMLHttpRequest();
        var apiBase = "";
        if(location.host.indexOf("localhost") >= 0){
            apiBase = "http://test.dakaqi.cn:3030/platform";
        }
        xhr.open("POST", apiBase + "/index/uploadFile", true);

        function callback() {
            if (xhr.readyState == 4) {
                $timeout(function() {
                    var data = angular.fromJson(xhr.responseText);
                    if (data.statusCode == -100) {
                        alert(data.statusMessage);
                        location.href = "#/login";
                    } else if (data.statusCode == -1) {
                        alert(data.statusMessage);
                        return;
                    }
                    $scope.$apply(function() {
                        item.img = data.statusMessage;
                    });
                }, 3000);
            }
        }
        xhr.onreadystatechange = callback;
        xhr.send(form);
    }
}]);